<?php
class PostAction extends CommonAction {

    public function index() {
        C('TOKEN_ON',false);
        //非登录用户不允许发布信息
        if(!isset($_COOKIE['user'])) $this->redirect('User/login');

        $nav = M('InfoNav');
        $allNav = $nav->where('pid=0 AND is_active=1')
                      ->field(array('id', 'name', 'pid'))
                      ->order('sort ASC')
                      ->select();

        $this->assign(array(
           'allnav'=>$allNav
        ));
        $this->display();
    }

    public function input() {

        //非登录用户不允许发布信息
        if(!isset($_COOKIE['user'])) $this->redirect('User/login');

        $nav = M('InfoNav');
        $attr = M('InfoAttr');
        $info = D('Info');
        $user = M('User');

        //获取用户资料
        $userInfo = $user->field(array('phone', 'real_name', 'qq', 'email'))->where("name='{$_COOKIE['user']}'")->find();

        $pid = $nav->where('id='.$_GET['id'])->getField('pid');
        $currentName = $nav->where('id='.$_GET['id'])->getField('name');
        $parent = $nav->where('id='.$pid)->getField('name');

        //列举信息的属性以及其类型
        $attrId = $nav->where('id='.$pid)->getField('attr');

        $map['id'] = array('in', $attrId);
        $temp = $attr->field(array('name', 'type', 'nickname', 'details'))
                        ->where($map)
                        ->select();

        $attrInfo = array();
        foreach($temp as $key=>$value) {
            $value['details'] = explode(',', $value['details']);
            $attrInfo[] = $value;
        }

        //发布信息
        if(isset($_POST['send'])) {
            if($info->create()) {
                //用户增加2积分
                $user->where("name='{$_COOKIE['user']}'")->setInc('score', 2);
                $info->add() ? $this->redirect('/Post/success') : $this->error('系统故障，发布失败!', 0);
            } else {
                $this->error($info->getError(), 0);
            }
        }

        $this->assign(array(
           'parent'=>$parent,
           'currentname'=>$currentName,
           'attr'=>$attrInfo,
           'userinfo'=>$userInfo,
        ));
        $this->display();
    }

    public function ajaxGetChild() {
        $id = $this->_post('id');
        $nav = M('InfoNav');
        $allChild = $nav->where("pid=$id AND is_active=1")
                        ->field(array('id', 'name'))
                        ->order('sort ASC')
                        ->select();
        echo json_encode($allChild);
    }

    public function success() {
        C('TOKEN_ON',false);
        $this->display();
    }

    public function ajaxCheckCode() {
        echo $_POST['code'] == session('code') ? 1 : 0;
    }

    public function ajaxCheckContentIsFilter() {
        $this->_isFilter('content');
    }

    public function ajaxCheckTitleIsFilter() {
        $this->_isFilter('title');
    }

    private function _isFilter($field) {
        $filterword = getSetting('filterword');
        $filterwordArr = explode(',', $filterword);
        foreach($filterwordArr as $key=>$value) {
            if(strpos($_POST[$field], $value) !== false) {
                echo 0;
                return ;
            }
        }
        echo 1;
    }


}

?>